package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-01
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private INursingLevelService nursingLevelService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 根据id查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = getById(id);
        List<NursingProjectPlanVo> nursingProjects= nursingProjectPlanMapper.selectByPlanId(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        nursingPlanVo.setProjectPlans(nursingProjects);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //添加护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        save(nursingPlan);
        //批量添加中间表的护理项目
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        int i = nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan.getId());
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i ;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        List<NursingProjectPlanVo> nursingProjects= nursingProjectPlanMapper.selectByPlanId(nursingPlan.getId());
        //护理项目列表不为空
        if(!nursingProjects.isEmpty()){
            //根据planId删除中间表数据
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            //批量添加中间表的护理项目
            nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(),nursingPlan.getId());
        }
        //修改护理计划
        int i = nursingPlanMapper.updateNursingPlan(nursingPlan);
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;
    }

    /**
     * 删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {

        //根据planid删除护理项目
        nursingProjectPlanMapper.deleteByPlanId(id);
        //删除护理计划
        List<NursingLevel> list = nursingLevelService.list();
        //判断是否关联护理等级
        for (NursingLevel nursingLevel : list) {
            if(nursingLevel.getLplanId() == id){
                return 0;
            }
        }
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        if(nursingPlan.getStatus() == 1){
            return 0;
        }
        int i = removeById(id) ? 1 : 0;
        if(i>0){
            redisTemplate.delete(CACHE_KEY_PREFIX);
        }
        return i;


    }

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> getPlanAll() {
        List<NursingPlanVo> nursingPlanVos = (List<NursingPlanVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(CollectionUtil.isNotEmpty(nursingPlanVos)){
            return nursingPlanVos;
        }
        nursingPlanVos = nursingPlanMapper.getPlanAll();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,nursingPlanVos);
        return nursingPlanVos;
    }


}
